package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.AttendanceMemberActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.ScheduleManagerActivity;
import com.lzx.iteam.bean.ScheduleManager;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;

/**
 * Created by Megan on 16/3/14.
 */
public class AttendanceMemberAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ScheduleManager> list;

    public AttendanceMemberAdapter(Context mContext) {
        this.mContext = mContext;

    }

    public void bindData(ArrayList<ScheduleManager> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list == null ? null : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        final ScheduleManager memberMsg = list.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.schedule_manager_item, null);
            holder.userImage = (ImageView) convertView.findViewById(R.id.iv_schedule_manager_image);
            holder.userName = (TextView) convertView.findViewById(R.id.tv_schedule_manager_name);
            holder.userPosition = (TextView) convertView.findViewById(R.id.tv_schedule_manager_position);
            holder.delete = (ImageView) convertView.findViewById(R.id.iv_schedule_manager_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.userName.setText(memberMsg.getUserName());
        ImageLoaderInterface.imageLoader.displayImage(memberMsg.getAvatar(), holder.userImage, ImageLoaderInterface.optionHeadImage);
        if (StringUtil.isEmpty(memberMsg.getJob())) {
            holder.userPosition.setVisibility(View.GONE);
        } else {
            holder.userPosition.setVisibility(View.VISIBLE);
            holder.userPosition.setText(memberMsg.getJob());
        }

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((AttendanceMemberActivity) mContext).delAttendanceMember(position);
            }
        });
        return convertView;
    }


    private class ViewHolder {
        ImageView userImage;
        TextView userName;
        TextView userPosition;
        ImageView delete;
    }

}
